package com.shigen.demo.atguigu;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

/**
 * 虚引用demo
 *
 * @author shigenfu
 * @date 2022/5/26 1:28 下午
 */
public class PhantomReferenceDemo {

  public static void main(String[] args) throws InterruptedException {
    Object o = new Object();
    ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
    PhantomReference<Object> phantomReference = new PhantomReference<>(o, referenceQueue);

    System.out.println(o);
    System.out.println(phantomReference.get());
    System.out.println(referenceQueue.poll());

    System.out.println("========");
    o = null;
    System.gc();
    Thread.sleep(1);
    System.out.println(o);
    System.out.println(phantomReference.get());
    System.out.println(referenceQueue.poll());

  }

}
